﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Data.Interfaces;
using Job.Logic.Common;
using Job.Entity;
using System.Data;
using Job.Web.Framework;

namespace Job.Logic.BaseContent
{
    public class AdminUserLogic
    {
        private readonly ICommon commonDao = DataAccess.commonDao;
        private readonly IJobUserDao dao = DataAccess.jobUserDao;

        public bool Insert(JobUser user)
        {
            IList<JobUser> users = dao.FindByLoginName(user.LoginName);
            if (users != null && users.Count > 0) return false;

            dao.Insert(user);
            return true;
        }

        public bool Update(JobUser user)
        {
            dao.Update(user);
            return true;
        }

        public bool UpdatePassword(JobUser user)
        {
            dao.UpdatePassword(user);
            return true;
        }

        public void DeleteById(int userId)
        {
            JobUser user = new JobUser();
            user.UserId = userId;
            dao.Delete(user);
        }

        public void DeleteByLogin(string loginName)
        {
            dao.DeleteByLoginName(loginName);
        }

        public JobUser GetDetailById(int userId)
        {
            return dao.Find(userId);
        }
        public JobUser GetDetailByLogin(string loginName)
        {
            IList<JobUser> list = dao.FindByLoginName(loginName);
            if (list == null) return null;
            if (list.Count > 1) return null;
            return list[0];
        }

        public int GetCount()
        {
            return dao.GetCount();
        }

        public DataTable GetUserPageList(int pageSize, int pageIndex)
        {
            string sql = @"Select user_id,login_name,name,email From job_user ";
            CommonLogic common = new CommonLogic();
            DataSet ds = common.GetPageDataBySql(sql, pageSize, pageIndex, 0,
                "user_id", SortDirection.Ascending, null);
            return ds.Tables[0];
        }

    }
}
